import { service, getData } from '@vimi/utils-api'
import { getApprove } from '@/credit/mixin/wf'
// import store from '@/credit/store'

// 批次审核状态
export const enumApprovalStatus = {
  0: '待审核',
  1: '审核中',
  2: '已通过',
  3: '已拒绝',
}

export const enumApprovalStatusStyle = {
  0: 'warning',
  1: 'info',
  2: 'success',
  3: 'danger',
}

export const approveBaseColumns = [
  { attrs: { label: '批次号', prop: 'batch_no', align: 'center' } },
  { attrs: { label: '项目数量', prop: 'project_amount', align: 'center' } },
  { attrs: { label: '授信金额(万元)', prop: 'project_funds', align: 'center' } },
  { attrs: { label: '支用金额(万元)', prop: 'cur_use_funds_total', align: 'center' } },
  { attrs: { label: '担保费率(%)', prop: 'gua_rate', align: 'center' } },
  { attrs: { label: '保费金额(元)', prop: 'gua_fee', align: 'center' } },
  { attrs: { label: '银行机构', prop: 'bank_name', align: 'center' } },
  {
    attrs: { label: '银行推荐日期', prop: 'create_time', align: 'center' },
    process: v => (v || '').substring(0, 10),
  },
  {
    attrs: { label: '立项日期', prop: 'claim_time', align: 'center' },
    process: v => (v || '').substring(0, 10),
  },
]

export const approveDataAdapter = item => {
  item.postloanStatus = enumApprovalStatus[item.approval_status] || ''
  item.postloanStatusStyle = enumApprovalStatusStyle[item.approval_status] || ''
  return item
}

export const tableName = 'customer_related_quick_batch'
const wfKey = 'QUICK_PROJECT_APPROVAL'

// 获取批量表
export const getRelatedBatch = batchId =>
  getData({
    table: tableName,
    params: {
      'customer_related_quick_batch_id.eq': batchId,
    },
  }).then(res => res?.data?.[0] ?? {})

// 获取审批流程配置信息
export const getApproveProcess = (queryId, user) =>
  getApprove(
    {
      wfKey,
      queryId,
      config: {
        'QUICK_PROJECT_APPROVAL-RESA': {
          cmpt: 'approve-common',
          rekey: null,
        },
        'QUICK_PROJECT_APPROVAL-RESB': {
          cmpt: 'approve-common',
          rekey: null,
        },
        'QUICK_PROJECT_APPROVAL-JURY_DIRECTOR': {
          cmpt: 'approve-common',
          rekey: 'QUICK_PROJECT_APPROVAL-RESA',
        },
      },
    },
    user
  )

// 正在审批中的任务
export const getInvingPerson = relatedList => {
  if (!relatedList.length) {
    return Promise.resolve({})
  }

  return getData({
    table: 'task_list',
    params: {
      'customer_related_id.in': relatedList.map(it => it.customer_related_quick_batch_id).join(','),
      'task_status.eq': 0,
      'workflow_key.eq': wfKey,
      fields: ['customer_related_id', 'task_person', 'workflow_key'],
    },
  }).then(res =>
    (res?.data || []).reduce((prev, curr) => {
      const { customer_related_id: relatedId } = curr
      if (!prev[relatedId]) prev[relatedId] = []
      prev[relatedId].push(curr)
      return prev
    }, {})
  )
}

// 获取省农担审批内容
export const getFollow = relatedList => {
  if (!relatedList.length) {
    return Promise.resolve({})
  }
  return service
    .get('/api/credit/wf/follow/', {
      params: {
        wfKey,
        wfRelatedId: relatedList.map(it => it.customer_related_quick_batch_id).join(','),
      },
    })
    .then(res => res?.data ?? [])
}
